import os
import tempfile

from flask import Blueprint, request, jsonify, url_for
from flask_login import login_required

from common.oss_upload import get_file_url, video_allowed_file, upload_file, image_allowed_file, generate_new_filename
from config import config_settings
from models.biz_enum import FileType

public = Blueprint('public', __name__, url_prefix='/public')


@public.route('/get_preview_url', methods=['GET'])
@login_required
def get_preview_url():
    file_name = request.args.get('file_name', '')
    if file_name:
        file_url = get_file_url(file_name, expires=600)
        return jsonify({"status": 'success', "msg": '', "data": {"path": file_url}})

    return jsonify({"status": 'fail', "msg": '', "data": {}})


@public.route('/upload_image', methods=['POST'])
@login_required
def upload_image():
    """上传文件到阿里云OSS"""
    if 'file' not in request.files:
        return jsonify({'error': '没有选择文件'}), 400

    file = request.files['file']
    if file.filename == '':
        return jsonify({'error': '没有选择文件'}), 400

    if file and image_allowed_file(file.filename):
        file_name, file_url = upload_file(file, FileType.IMAGE)
        if file_url:
            return jsonify({'name': file_name, 'url': file_url})
        else:
            return jsonify({'error': '上传失败'}), 500

    return jsonify({'error': '文件类型不支持'}), 400


@public.route('/temp_upload_video', methods=['POST'])
@login_required
def temp_upload_video():
    """上传文件到阿里云OSS"""
    if 'file' not in request.files:
        return jsonify({'error': '没有选择文件'}), 400

    file = request.files['file']
    if file.filename == '':
        return jsonify({'error': '没有选择文件'}), 400

    if file and video_allowed_file(file.filename):
        try:
            # 生成临时文件路径
            file_name = generate_new_filename(file.filename, FileType.VIDEO)

            temp_filename = generate_new_filename(file.filename)

            temp_file_path = os.path.join(config_settings.VIDEO_UPLOAD_FOLDER, temp_filename)
            file.save(temp_file_path)

            file_url = url_for('uploads_file.videos', filename=temp_filename)

            return jsonify({'name': file_name, 'temp_name': temp_filename, 'url': file_url})

        except Exception as e:
            print(e)
            return jsonify({'error': '上传失败'}), 500

    return jsonify({'error': '文件类型不支持'}), 400

#
# @public.route('/upload_video', methods=['POST'])
# @login_required
# def upload_video():
#     """上传文件到阿里云OSS"""
#     if 'file' not in request.files:
#         return jsonify({'error': '没有选择文件'}), 400
#
#     file = request.files['file']
#     if file.filename == '':
#         return jsonify({'error': '没有选择文件'}), 400
#
#     print(file.filename)
#
#     if file and video_allowed_file(file.filename):
#         file_name, file_url = upload_file_async(file, FileType.VIDEO)
#         if file_url:
#             return jsonify({'name': file_name, 'url': file_url})
#         else:
#             return jsonify({'error': '上传失败'}), 500
#
#     return jsonify({'error': '文件类型不支持'}), 400
